package cn.snmp.mutual.uk.stack;

import java.io.*;

class AsnDecoderBase {
    private static final String version_id = "@(#)$Id: AsnDecoderBase.java,v 3.3 2007/10/17 10:36:47 birgita Exp $ Copyright Westhawk Ltd";

    AsnSequence getAsnSequence(final InputStream in) throws IOException, DecodingException {
        AsnSequence asnTopSeq = null;
        final AsnSequence dummy = new AsnSequence();
        final AsnObject obj = dummy.AsnReadHeader(in);
        if (obj instanceof AsnSequence) {
            asnTopSeq = (AsnSequence) obj;
            return asnTopSeq;
        }
        String msg = "AsnSequence was expected";
        if (obj != null) {
            msg = msg + " instead of " + obj.getRespTypeString();
        } else {
            msg += ", but is null";
        }
        throw new DecodingException(msg);
    }

    int getSNMPVersion(final AsnSequence asnTopSeq) throws DecodingException {
        int version = -1;
        final AsnObject obj = asnTopSeq.getObj(0);
        if (obj instanceof AsnInteger) {
            final AsnInteger v = (AsnInteger) obj;
            version = v.getValue();
            return version;
        }
        final String msg = "SNMP version should be of type AsnInteger instead of " + obj.getRespTypeString();
        throw new DecodingException(msg);
    }

    String getCommunity(final AsnSequence asnTopSeq) throws DecodingException {
        String comm = "";
        final AsnObject obj = asnTopSeq.getObj(1);
        if (obj instanceof AsnOctets) {
            final AsnOctets estat = (AsnOctets) obj;
            comm = estat.getValue();
            return comm;
        }
        final String msg = "community should be of type AsnOctets instead of " + obj.getRespTypeString();
        throw new DecodingException(msg);
    }

    AsnSequence getAsnHeaderData(final AsnSequence asnTopSeq) throws DecodingException {
        AsnSequence asnHeaderData = null;
        final AsnObject obj = asnTopSeq.getObj(1);
        if (obj instanceof AsnSequence) {
            asnHeaderData = (AsnSequence) obj;
            return asnHeaderData;
        }
        final String msg = "asnHeaderData should be of type AsnSequence instead of " + obj.getRespTypeString();
        throw new DecodingException(msg);
    }
}
